Because the data type model in Sparkplug is not identical with the .NET type model, you need to understand what the type correspondences are, and in some cases the Sparkplug data types are represented by .NET types that differ from intuitive expectations. To learn about the representation of Sparkplug data types in Connectivity Software, see DataTypes in Rapid Toolkit for Sparkplug.
By default, the DataType Property of a SparkplugMetric (e.g. created with a default constructor) is set to Unknown. In Sparkplug, Unknown is a data type placeholder for future expansion. Rapid Toolkit for Sparkplug uses Unknown in the DataType Property as an indication that the data type of the metric is not known upfront, and should be determined based on the actual metric data being passed to or from the metric. This is a special behavior reserved for specific use cases. For Sparkplug compliance and interoperability, it is strongly recommended that you always set the DataType Property to a specific data type other than Unknown.
You can set the DataType Property explicitly from your code, However, when you use the extension methods for Sparkplug Metric Configuration, many of them either allow you to pass in the data type as a parameter, or they actually infer the data type automatically (in compile-time).
Refresher: In the context of Sparkplug edge node/device development, by "data provision", we understand the process through which your code provides data that is published by the Spakrplug edge node or device, ans subsequently subscribed to by Sparkplug host applications.
When you use the extension method for Sparkplug Metric Configuration, and provide a function that handles the Read request, Rapid Toolkit for Sparkplug determines the Sparkplug data type of the metric from the .NET type of the function argument. For example, if you use the simplest ReadValueFunction method overload and simply pass it a function that returns System.Int32 type, the Sparkplug data type (the DataType Property of the metric) will be automatically determined to be SparkplugDataType.Int32.
There are cases, however, when this automatic mechanism is not suitable; if so, you can still use the extension methods for metric configuration, but you have to specify the desired data type in some additional ways. This happens e.g. when multiple Sparkplug data types are mapped to a single .NET type (as per Data Types in Rapid Toolkit for Sparkplug article). In the example given above, Rapid Toolkit for Sparkplug uses System.Int32 not only for Sparkplug's Int32 data type, but also for UInt16 (this is for it to be CLS-compliant and usable from languages like VB.NET; similarly with some other integer types). If you want your metric be of Sparkplug Int8, UInt16, UInt32 or UInt64 data type, you can do it similarly to the following example.
Example
Also, in Sparkplug, there is a Bytes type, which physically is the same as UInt8Array. When Rapid Toolkit for Sparkplug methods encounter a .NET array of bytes, they prefer to choose the Sparkplug Bytes data type for it, unless specified otherwise. The following example illustrates an implementation of a readable metric with Sparkplug data type Bytes.
Example
In the Push Data Provision Model, the concerns described above with the Pull Data Provision Model still apply. However, since in the configuration methods there is no function provided with an argument from which the data type can be automatically determined, you will end up specifying the data type yourself, typically by some overload of the ValueType Method.
Example
Refresher: In the context of Sparkplug edge node/device development, by "data consumption", we understand the process through which your code consumes the commands that it receives from Sparkplug host applications.
When you use some extension method for Sparkplug Metric Configuration, and provide a function (or action) that handles the Write request, Rapid Toolkit for Sparkplug determines the Sparkplug data type of the metric from the .NET type of the function (or action) argument. For example, if you use the simplest WriteValueAction method overload and simply pass it an action that takes System.Int32 argument, the Sparkplug data type (the DataType Property of the metric) will be automatically determined to be SparkplugDataType.Int32.
There are cases, however, when this automatic mechanism is not suitable; if so, you can still use the extension methods for metric configuration, but you have to specify the desired data type in some additional ways. This happens e.g. when multiple Sparkplug data types are mapped to a single .NET type (as per Data Types in Rapid Toolkit for Sparkplug article). In the example given above, Rapid Toolkit for Sparkplug uses System.Int32 not only for Sparkplug's Int32 data type, but also for UInt16 (this is for it to be CLS-compliant and usable from languages like VB.NET; similarly with some other integer types). If you want your metric be of Sparkplug Int8, UInt16, UInt32 or UInt64 data type, you can do it similarly to the following example.
Example
Also, in Sparkplug, there is a Bytes type, which physically is the same as UInt8Array. When Rapid Toolkit for Sparkplug methods encounter a .NET array of bytes, they prefer to choose the Sparkplug Bytes data type for it, unless specified otherwise. The following example illustrates an implementation of a writable metric with Sparkplug data type Bytes.
Example
In the Pull Data Consumption Model, the concerns described above with the Push Data Consumption Model still apply. However, since in the configuration methods there is no function or action provided with an argument from which the data type can be automatically determines, you will end up specifying the data type yourself, typically by some overload of the ValueType Method.
Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.